package com.android.hedgehog;

import java.util.List;
import java.util.Random;

import android.graphics.Color;

import com.android.hedgehog.framework.Game;
import com.android.hedgehog.framework.Graphics;
import com.android.hedgehog.framework.Screen;
import com.android.hedgehog.framework.Input.TouchEvent;
import com.android.hedgehog.puzzles.CardMatchScreen;
import com.android.hedgehog.puzzles.CardMatchScreen2;
import com.android.hedgehog.puzzles.GoalieScreen;
import com.android.hedgehog.puzzles.GoalieScreen2;
import com.android.hedgehog.puzzles.MathScreen;
import com.android.hedgehog.puzzles.PuzzleScreen;
import com.android.hedgehog.puzzles.SeesawScreen2;
import com.android.hedgehog.puzzles.TimeBombScreen;

public class FreePlayScreen2 extends Screen {
	enum GameStatus {
		Launch, Running, Paused, GameOver
	}

	public static final int FREEPLAYTIMELIMIT = 100; // total play time
	public double currentTime = 0.0; // total elapsed time so far
	public int totalScore; // total points by player
	public Game game;
	public Graphics graphics;
	public String[] puzzleTitles = { "Math", "Card", "Goalie", "TimeBomb", "Seesaw" };
	public PuzzleScreen puzzle;
	Random rand = new Random();
	int modeNumber; // 0=Easy, 1=Normal, 2=Hard
	int[] easyMode = { 1, 2, 2, 2, 3, 3, 3, 3, 4, 4, 4, 5 };
	int[] normalMode = { 1, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 5, 5, 5, 5, 5, 5, 6, 6, 6, 6, 6, 6, 7, 7, 7,
			7, 7, 7, 8, 8, 8, 8, 9, 9, 10 };
	int[] hardMode = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
	int gameNumber;
	int len;
	int currentInstanceNum;
	GameStatus state;

	public FreePlayScreen2(Game game, int gameNumber, int modeNumber) {
		super(game);
		this.game = game;
		graphics = game.getGraphics();
		this.gameNumber = gameNumber;
		this.modeNumber = modeNumber;
		state = GameStatus.Launch;
		currentInstanceNum = 1;
		totalScore = 0;
	}

	@Override
	public void update(float deltaTime) {

		if (state == GameStatus.Launch) {
			startPuzzle();
			state = GameStatus.Running;
		}

		if (state != GameStatus.Paused) {// could change this <
			currentTime += deltaTime;
		}

		if (currentTime > FREEPLAYTIMELIMIT) {
			totalScore += puzzle.instanceScore; // last second special score
			game.setScreen(new FreePlayResult(game, gameNumber, totalScore));
		}

		if (puzzle.instanceOver) {
			totalScore += puzzle.instanceScore;
			currentInstanceNum++;
			state = GameStatus.Launch;
		}

		List<TouchEvent> touchEvents = game.getInput().getTouchEvents();
		game.getInput().getKeyEvents();

		if (state == GameStatus.Paused)
			updatePaused(touchEvents);
		if (state == GameStatus.Running) {
			updateRunning(touchEvents);
			puzzle.update(touchEvents, deltaTime);
		}

	}

	private void startPuzzle() {
		int difficulty;

		if (modeNumber == 0) {// easy
			difficulty = easyMode[rand.nextInt(easyMode.length)];
		} else if (modeNumber == 1) {
			difficulty = normalMode[rand.nextInt(normalMode.length)];
		} else {
			difficulty = hardMode[rand.nextInt(hardMode.length)];
		}

		// gameNumber = 2; // test

		if (gameNumber == 0)
			puzzle = new MathScreen(game, 1, difficulty, currentInstanceNum);
		if (gameNumber == 1)
			puzzle = new CardMatchScreen2(game, 1, difficulty, currentInstanceNum);
		if (gameNumber == 2)
			puzzle = new GoalieScreen2(game, 1, difficulty, currentInstanceNum);

		if (gameNumber == 3)
			puzzle = new TimeBombScreen(game, 1, difficulty, currentInstanceNum);
		//if (gameNumber == 4)
			//puzzle = new SeesawScreen2(game, 1, difficulty, currentInstanceNum);

	}

	private void updatePaused(List<TouchEvent> touchEvents) {

		len = touchEvents.size();
		for (int i = 0; i < len; i++) {// FOR
			TouchEvent event = touchEvents.get(i);
			if (event.type == TouchEvent.TOUCH_UP) { // TOUCH UP

				if (event.y > 135 && event.y < 185) { // RESUME
					if (Settings.soundEnabled)
						Assets.click.play(1);
					state = GameStatus.Running;
					return;
				} // RESUME

				if (event.x > 70 && event.x < 245 && event.y > 255 && event.y < 305) { // QUIT
					game.setScreen(new MainMenu2(game));
					if (Settings.soundEnabled)
						Assets.click.play(1);
					return;
				}// QUIT

			}
		}
	}// UPDATE PAUSED

	private void updateRunning(List<TouchEvent> touchEvents) {

		len = touchEvents.size();
		for (int i = 0; i < len; i++) {// FOR
			TouchEvent event = touchEvents.get(i);
			if (event.type == TouchEvent.TOUCH_UP) { // TOUCH UP

				if (event.x < 30 && event.y < 30) { // Pause Button
					if (Settings.soundEnabled)
						Assets.click.play(1);
					state = GameStatus.Paused; // goto Paused state
					return;
				} // Pause Button

			}// TOUCH UP;
		}// FOR

	}

	@Override
	public void present(float deltaTime) {

		graphics.drawPixmap(Assets.blackbackground, 0, 0); // use clear instead

		if (state == GameStatus.Running) {
			graphics.drawText("" + (totalScore + puzzle.instanceScore), 60, 20, 20, Color.WHITE); // score
			graphics.drawText("" + (int) (FREEPLAYTIMELIMIT - currentTime), 260, 20, 20, Color.WHITE); // time.
			graphics.drawPixmap(Assets.whitebuttons, 0, 0, 0, 0, 30, 30);// pause
			graphics.drawPixmap(Assets.whitebuttons, 290, 0, 30, 0, 30, 30);// restart
			graphics.drawSpecialText(puzzleTitles[gameNumber], 160, 20, 20, 1, Color.MAGENTA);
			graphics.drawLine(0, 30, 320, 30, Color.WHITE);
			puzzle.present();
		}
		if (state == GameStatus.Paused) {
			graphics.drawSpecialText("RESUME", 160, 180, 80, 2, Color.GREEN);
			graphics.drawSpecialText("QUIT", 160, 300, 80, 2, Color.RED);
		}

	}

	@Override
	public void pause() {
		// TODO Auto-generated method stub

	}

	@Override
	public void resume() {
		// TODO Auto-generated method stub

	}

	@Override
	public void dispose() {
		// TODO Auto-generated method stub

	}

}